package com.fcscs.cservice.client.main.configuration.inroomdining;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedListener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.Label;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.form.ComboBox.TriggerAction;
import com.fcscs.core.common.authority.UserAuthority;
import com.fcscs.core.gwt.common.AsyncCallbackListener;
import com.fcscs.core.gwt.common.CoreConstants;
import com.fcscs.core.gwt.common.CoreEnums.SubmitType;
import com.fcscs.core.gwt.common.FormSubmitListener;
import com.fcscs.core.gwt.common.SessionContext;
import com.fcscs.core.gwt.common.ui.CenteredInfo;
import com.fcscs.core.gwt.common.ui.FcsFields.EasyClearCombobox;
import com.fcscs.core.gwt.common.ui.FcsFields.FormButton;
import com.fcscs.core.gwt.common.ui.FcsFormPanel;
import com.fcscs.core.gwt.common.ui.FcsPanel;
import com.fcscs.cservice.client.common.CServiceConstants;
import com.fcscs.cservice.client.common.CserviceControllerUtils;
import com.fcscs.cservice.client.common.MessageBoxTools;
import com.fcscs.cservice.client.common.controller.InRoomServiceController;
import com.fcscs.cservice.client.common.model.configuration.inroomdining.BillOptionModelData;
import com.fcscs.cservice.client.common.model.configuration.reservation.PropertyIdNameModelData;
import com.fcscs.cservice.domain.BillOption;
import com.google.gwt.user.client.ui.Widget;

public class BillOptionPanel extends FcsPanel implements FormSubmitListener<BillOption>{

	private VerticalPanel vp;
	private EasyClearCombobox<PropertyIdNameModelData> propCombox;
	private ListStore<PropertyIdNameModelData> propListStore;
	private BillOptionGrid grid;
	private BillOptionDialog billOptionDialog;
	private FormButton addBtn;
	private long propId;
	private BillOption currentBillOption;

	public BillOptionPanel(String id) {
		super(id);
	}

	@Override
	public void init() {
		if (null == vp) {
			vp = new VerticalPanel();
			vp.setId("BillOptionPanel_vp");
			vp.setSpacing(5);

			HorizontalPanel hp = new HorizontalPanel();
			hp.setId("BillOptionPanel_hp");
			hp.setSpacing(5);

			Label propLabel = new Label(CoreConstants.CORE_CONSTANTS.property()
					+ CoreConstants.REQURIED_FIELD_LABEL);
			propLabel.setId("BillOptionPanel_propLabel");
			propLabel.setWidth(150);

			propListStore = new ListStore<PropertyIdNameModelData>();
			propCombox = new EasyClearCombobox<PropertyIdNameModelData>();
			propCombox.setId("BillOptionPanel_propCombox");
			// propCombox.setFieldLabel(CoreConstants.CORE_CONSTANTS.property());
			propCombox.setWidth(200);
			propCombox.setDisplayField(CoreConstants.FULLTNAME);
			propCombox.setAllowBlank(false);
			propCombox.setForceSelection(true);
			propCombox.setTriggerAction(TriggerAction.ALL);
			propCombox.setStore(propListStore);
			propCombox.addSelectionChangedListener(new SelectionChangedListener<PropertyIdNameModelData>() {

				@Override
				public void selectionChanged(SelectionChangedEvent<PropertyIdNameModelData> se) {
					PropertyIdNameModelData pinmmd = propCombox.getValue();
					if (pinmmd != null) {
						propId = pinmmd.getEntity().getId();
						newSearch();
					}
				}
			});

			hp.add(propLabel);
			hp.add(propCombox);

			grid = BillOptionGrid.getInstance(CServiceConstants.CSERVICE_CONSTANTS.billOption(), null,propCombox.getValue());

			vp.add(hp);
			vp.add(grid);

			addBtn = new FormButton(CoreConstants.CORE_MESSAGES.operation(CoreConstants.CORE_CONSTANTS.add(),
					CServiceConstants.CSERVICE_CONSTANTS.billOption()));
			addBtn.setStyleAttribute(CServiceConstants.CSS_MARGINLEFT, "5px");
			addBtn.addSelectionListener(new SelectionListener<ButtonEvent>() {
				@Override
				public void componentSelected(ButtonEvent ce) {
					showAddDialog();
				}
			});
			vp.add(addBtn);

			add(vp);

			onShow();
		}
	}
	private void showAddDialog() {
		currentBillOption=new BillOption();
		currentBillOption.setPropertyId(propId);
		billOptionDialog = BillOptionDialog.getInstance(true,this,propCombox.getValue());
		billOptionDialog.getFormPanel().updateForm(currentBillOption);
		billOptionDialog.show();
	}
	@Override
	public UserAuthority getRequiredAuthority() {
		return null;
	}

	@Override
	protected Widget getViewPanel() {
		return null;
	}

	@Override
	public void onShow() {
		CserviceControllerUtils.loadPropertys(propListStore, propCombox);
	}
	
	public void newSearch(){
		grid.removeAllModels();
		List<BillOptionModelData> payList = new ArrayList<BillOptionModelData>();
		BillOption model1 = new BillOption();
		model1.setName("bill1");
		model1.setNameSecLang("code1");
		payList.add(new BillOptionModelData(model1));

		BillOption model2 = new BillOption();
		model2.setName("bill2");
		model2.setNameSecLang("code2");
		payList.add(new BillOptionModelData(model2));
		grid.addModels(payList);
		InRoomServiceController.getINSTANCE().getBillOption(SessionContext.getInstance().getOrgId(), propId, new AsyncCallbackListener<List<BillOption>>(){

			@Override
			public void onLogin(String username) {
				InRoomServiceController.getINSTANCE().getBillOption(SessionContext.getInstance().getOrgId(), propId,this);
			}

			@Override
			public void onFailure(Throwable caught) {
				MessageBoxTools.messageAlertSystemError();
			}

			@Override
			public void onSuccess(List<BillOption> result) {
				if (result != null) {
					List<BillOptionModelData> models = new ArrayList<BillOptionModelData>();
					for (BillOption jf : result) {
						models.add(new BillOptionModelData(jf));
					}
					grid.removeAllModels();
					grid.addModels(models);
				} else {
					MessageBoxTools.messageAlertSystemError();
				}
			}
			
		});
	}
	

	@Override
	public boolean onHide() {
		return true;
	}

	@Override
	public void formSubmitCallBack(FcsFormPanel<BillOption> form,
			SubmitType submitType) {
		billOptionDialog.getFormPanel().populateEntity(currentBillOption);
		saveBillOption(currentBillOption);
	}

	private void saveBillOption(final BillOption currentBillOption2) {
		InRoomServiceController.getINSTANCE().createBillOption(currentBillOption2, new AsyncCallbackListener<BillOption>(){

			@Override
			public void onLogin(String username) {
				InRoomServiceController.getINSTANCE().createBillOption(currentBillOption2,this);
			}

			@Override
			public void onFailure(Throwable caught) {
				MessageBoxTools.messageAlertSystemError();
			}

			@Override
			public void onSuccess(BillOption result) {
				if (result != null) {
					grid.addModel(new BillOptionModelData(result));
					billOptionDialog.hide();
					CenteredInfo.displayCompleted(CoreConstants.CORE_CONSTANTS.added());
				} else {
					MessageBoxTools.messageAlertSystemError();
				}
			}
		});
	}

}
